﻿#include "userservice.h"
#include <QMessageBox>
#include "user.h"

UserService::UserService(QObject *parent) : QObject(parent)
{
    inputDialog.setWindowTitle("输入操作密码");
    inputDialog.setLabelText("请输入操作密码！");
    inputDialog.setOkButtonText("确定");
    inputDialog.setCancelButtonText("取消");
    inputDialog.setTextEchoMode(QLineEdit::Password);
}

void UserService::saveUser(QWidget *widget, QString nickName, QString position, QString tel, QString mobile, QString QQ, QString wechat)
{
    bool ok;
    QString res = QInputDialog::getText(widget, "输入",
                                        "请输入操作密码", QLineEdit::Password,
                                        "", &ok);

    if(ok){
        if(res == User::getOperatePassword()){
            manager = new UserNetworkAccessManager();

            connect(manager, &BaseNetworkAccessManager::onSuccess,
                    [=](const QString message)
            {
                QMessageBox::information(widget, "消息", message);
            });

            connect(manager, &BaseNetworkAccessManager::onFailed,
                    [=](const QString message)
            {
                QMessageBox::warning(widget, "警告", message);
            });

            manager->updateUser(
                        nickName,
                        position,
                        tel,
                        mobile,
                        QQ,
                        wechat);
        }else{
            QMessageBox::warning(widget, "警告", "密码错误");
        }
    }
}

bool UserService::inputOperatePassword(QWidget *widget){
    inputDialog.setTextValue("");
    if(inputDialog.exec()){
        QString res = inputDialog.textValue();
        if(res == User::getOperatePassword()){
            return true;
        }else{
            QMessageBox::warning(widget, "警告", "密码错误");
            return false;
        }
    }

    return false;
}

/**
 * 检查当前用户是否已加入企业
 * @brief checkUserCompany
 * @return
 */
bool UserService::checkUserCompany(QWidget *widget){
    if(User::getCompanyId().isEmpty()){
        QMessageBox::warning(widget, "警告", "您还未加入企业，无法使用该功能！");
        return false;
    }

    return true;
}

void UserService::sendMessage(QWidget *widget, QString mobile)
{
    if(mobile.isEmpty()){
        QMessageBox::warning(widget, "警告", "输入手机号不能为空！");
        return;
    }

    manager = new UserNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onSuccessWithData,
            [=](QString message, QJsonObject data)
    {
        QString resCode = data.value("resCode").toString();
        QMessageBox::information(widget, "消息", message);
        emit sendMessageSuccess(resCode);
    });

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](const QString message)
    {
        QMessageBox::warning(widget, "警告", message);
    });

    manager->sendMessage(mobile);
}

void UserService::findPassword(
        QWidget *widget,
        QString username,
        QString password1,
        QString password2){
    if(username.isEmpty() || password1.isEmpty()){
        QMessageBox::warning(widget, "警告", "用户名或密码不能为空！");
        return;
    }

    if(password1 != password2){
        QMessageBox::warning(widget, "警告", "请输入相同的密码！");
        return;
    }

    manager = new UserNetworkAccessManager();

    connect(manager, &BaseNetworkAccessManager::onFailed,
            [=](QString message)
    {
        QMessageBox::warning(widget, "警告", message);
    });

    connect(manager, &BaseNetworkAccessManager::onSuccess,
            [=](QString message)
    {
        QMessageBox::information(widget, "信息", message);
        widget->close();
    });

    manager->findPassword(username, password1);
}
